﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class Admin_QuanLyQuanTriVien_EditUser : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    private Entities en = new Entities();
    private string strID = string.Empty;
    private string p = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
        {
            strID = Request.QueryString["id"].ToString();
            txtID.Enabled = false;
        }
        if (!IsPostBack)
        {
            this.GetQTinfo(strID);
            txtpass.Text = "******";
            txtpass.Enabled = false;
        }
    }
    protected void GetQTinfo(string id)
    {
        SqlDataReader drd;
        SqlCommand comm = new SqlCommand("getQTbyID", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@ID", id);
        comm.Connection.Open();
        drd = comm.ExecuteReader();
        if (drd.Read())
        {
            this.txtID.Text = drd["ID"].ToString();
            this.txtusername.Text = drd["username"].ToString();
            p = drd["PassWord"].ToString();

        }
        drd.Close();
        drd.Dispose();
        comm.Connection.Close();
        comm.Connection.Dispose();

    }
    protected void NV_Edit_Click(object sender, EventArgs e)
    {
        string username = txtusername.Text;
        string pass = txtnewpass.Text;
        string uppass = "";
        if (pass.Equals(""))
        {
            uppass = p;
        }
        else {
            uppass = pass;
        }
        SqlCommand comm = new SqlCommand("updateQT", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@id", strID);
        comm.Parameters.AddWithValue("@username", username);
        comm.Parameters.AddWithValue("@pass", en.EncodePassword(uppass,""));

        comm.Connection.Open();
        comm.ExecuteNonQuery();

        comm.Connection.Close();
        comm.Connection.Dispose();
        Response.Redirect("Danhsachquantri.aspx");
    }
}
